/**
 *lileilei
 * 2019��1��18��
 * ExcutionSql.java
 */
package common;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import config.commonconfig;
import org.apache.http.ContentTooLongException;

public class ExcutionSql {
	Statement cStatement;
	Connection conn ;
	public ExcutionSql() throws SQLException, ClassNotFoundException{
		Class.forName("com.mysql.jdbc.Driver");
        String url = String.format("jdbc:mysql://%s:%s/%s", commonconfig.sqlhost, commonconfig.sqlport,commonconfig.sqldata);
        conn= DriverManager.getConnection(url, commonconfig.sqluser,
                commonconfig.sqlpassword);
        cStatement = conn.createStatement();
	}
	
	/*����sql*/
	public int insert(String sql) throws SQLException {
		int i=cStatement.executeUpdate(sql);
		return i;
	}
	
	/*��ѯsql*/
	public  List<Map<String, String>> getresult(String sql) throws SQLException {
		PreparedStatement pst = null;
        List<Map<String,String>> list = new ArrayList<Map<String,String>>();
        Map<String, String> data = new HashMap<String, String>();
        try {
            pst = (PreparedStatement) conn.prepareStatement(sql);
            ResultSet rs = pst.executeQuery();
            ResultSetMetaData rsmd = pst.getMetaData();
            int size = rsmd.getColumnCount();
            while (rs.next()) {
            	for (int i = 1; i <= size; i++) {
                    data.put(rsmd.getColumnName(i),rs.getString(rsmd.getColumnName(i)));
                }
                list.add(data);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
	}
}
